package com.jshare.service.Impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jshare.dao.ArCommentDAO;
import com.jshare.dao.ArticleDAO;
import com.jshare.dao.Impl.ArticleDaoImpl;
import com.jshare.entity.ArComment;
import com.jshare.entity.Article;
import com.jshare.entity.PageBean;
import com.jshare.service.ArticleServive;

/**
 * 
 * @author 陆意
 * @version 1.0
 * 
 */
@Service("articleService")
public class ArticleServiceImp implements ArticleServive {

	@Resource(name = "articleDao")
	private ArticleDAO articledao;
	@Resource(name = "arCommentDao")
	private ArCommentDAO arcommentdao;
	List list;
	Article article;

	/**
	 * 发表评论
	 */
	public void putComment(ArComment arcomment,int articleId) {
		
		System.out.println("service层运行");
		try {
			arcommentdao.putComment(arcomment, articleId);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	/**
	 * 根据文章id找到文章
	 */
	public Article findById(int articleId) {

		article = this.articledao.findById(articleId);
		return article;
	}

	/**
	 * 分页查询
	 * 
	 * @param currentPage
	 *            当前第几页
	 * @param pageSize
	 *            每页大小
	 * @return 封闭了分页信息(包括记录集list)的Bean
	 */
	public PageBean queryForPage(int pageSize, int page) {

		final String hql = "from Article"; // 查询语句
		int allRow = articledao.getAllRowCount(hql); // 总记录数
		int totalPage = PageBean.countTotalPage(pageSize, allRow); // 总页数
		final int offset = PageBean.countOffset(pageSize, page); // 当前页开始记录
		final int length = pageSize; // 每页记录数
		final int currentPage = PageBean.countCurrentPage(page);
		List<Article> list = articledao.queryForPage(hql, offset, length); // "一页"的记录

		// 把分页信息保存到Bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		return pageBean;
	}

	public void addArticle(Article article) {

		try {
			articledao.save(article);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List findAll() {

		try {

			list = articledao.findAll();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return list;

	}

	public ArticleDAO getArticledao() {
		return articledao;
	}

	public void setArticledao(ArticleDAO articledao) {
		this.articledao = articledao;
	}

	public ArCommentDAO getArcommentdao() {
		return arcommentdao;
	}

	public void setArcommentdao(ArCommentDAO arcommentdao) {
		this.arcommentdao = arcommentdao;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

}
